package com.lovo.bbs.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *  提供连接MYSQL数据库连接对象的一个类 2008/06/25
 */
 public class MysqlDb implements IdbUtil {
	 boolean isPool=false;
	 
	// 取得连接的方法
	public Connection getConn() throws NamingException, SQLException {
		Connection conn = null;
		
		if(this.isPool){
			Context initCtx = new InitialContext();
			Context envCtx = (Context)initCtx.lookup( "java:comp/env" );
			DataSource ds = (DataSource)envCtx.lookup( "jdbc/mysql" );
			conn = ds.getConnection();
			

			
		}else{
		}
		return conn;
	}


	// 关闭连接的方法
	public void closeConnection(Connection con) {
		try {
			con.close();
		
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	//关闭结果集的方法
	public void closeRS(ResultSet rs){
	     try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//关闭语句对象的方法
	public void closePS(PreparedStatement ps){
		try {
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public boolean isPool() {
		return isPool;
	}


	public void setPool(boolean isPool) {
		this.isPool = isPool;
	}

}
